<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of controller
 *
 * @author Christian Simon
 */
class controllerRank {

    public static function get($int) {
        $sql = "SELECT * FROM  `rank` where `_id` = " . $int;
        $query = MySql::query($sql);
        while ($row = mysql_fetch_object($query)) {
            $rank = new modelRank();
            return $rank->deserialize($row);
        }
    }

    public static function add($rank) {
        $sql = "INSERT INTO `vdl_roleplay`.`rank` (`_id`, `_oid`, `_rid`, `name`) 
            VALUES (NULL," .
                $rank->_oid . ", " .
                $rank->_rid . ", \"" .
                $rank->name . "\");";
        MySql::insert($sql);
        return mysql_affected_rows();
    }

    public static function delete($int) {
        $sql = "DELETE FROM  `rank` where `_id` = " . $int;
        MySql::query($sql);
        return mysql_affected_rows();
    }

    private static function getMemberCount($int)
    {
        $sql = "Select count(*) from `character` where `_rid` = ".$int;
        $query = MySql::query($sql);
        $row = mysql_fetch_row($query);
        return $row[0];
    }
    
    public static function getNameById($int)
    {
        $sql = "Select `name` from `rank` where `_id` = ".$int;
        $query = MySql::query($sql);
        if(empty($query))
        {
            return "-";
        }
        else
        {
            $row = mysql_fetch_row($query);
            return $row[0];
        } 
    }
    
    public static function getAdminRow() {
        $sql = "SELECT * from `rank`";
        $query = MySql::query($sql);
        while ($row = mysql_fetch_object($query)) {
            echo "<tr>";
            echo "<td>";
            echo htmlentities($row->name, ENT_QUOTES, 'UTF-8');
            echo "</td><td>";
            echo htmlentities(controllerOrganisation::getNameById($row->_oid), ENT_QUOTES, 'UTF-8');
            echo "</td><td>";
            echo self::getMemberCount($row->_id);
            echo "</td><td>";
            echo htmlentities(self::getNameById($row->_rid), ENT_QUOTES, 'UTF-8');
            echo "</td><td>";
            echo "<a href='#' onclick='loadTabContent(\"./view/admin.rank.edit.php\",\"" . $row->_id . "\")'><img title='" . htmlentities($row->name, ENT_QUOTES, 'UTF-8') . " bearbeiten' src='./res/icons/pencil.png'></a>";
            echo "</td><td>";
            echo "<a href='#' onclick='removeRank(" . $row->_id . ")'><img title='" . htmlentities($row->name, ENT_QUOTES, 'UTF-8') . " löschen' src='./res/icons/close.png'></a>";
            echo "</td>";
            echo "</tr>";
        }
    }

}

?>
